import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'


//引用拦截器
import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import { initMenu } from './utils/menu'
import {downloadRequest} from './utils/download'

//使用前加上this.否则无法使用
Vue.prototype.postRequest=postRequest;
Vue.prototype.putRequest=putRequest;
Vue.prototype.deleteRequest=deleteRequest;
Vue.prototype.getRequest=getRequest;
Vue.prototype.downloadRequest=downloadRequest;

router.beforeEach((to,from,next)=>{
	if(window.sessionStorage.getItem('tokenStr')){
		initMenu(router,store);
		if(!window.sessionStorage.getItem('user')){
			return getRequest('/admin/info').then(resp=>{
				if(resp){
					console.log(resp)
					window.sessionStorage.setItem('user',JSON.stringify(resp))
					store.commit('INIT_ADMIN',resp)
					//此处如果单独写next()会出现点两次才能登录的情况
					next({ ...to,replace: true })
				}
			})
		}
		next();
	}else{
		if(to.path=='/'){
			next();
		}else{
			next('/?redirect='+to.path);
		}
			
	}
	
})


Vue.config.productionTip = false
Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
